﻿Imports Microsoft.Xna.Framework

Namespace Global.Runa.UI.Animation

    Public Class ColorAnimation
        Inherits AnimationBase

        Public From As Nullable(Of Color)
        Public [To] As Color

        Private Value As Vector4
        Private Incremental As Vector4

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal fps As Single)
            MyBase.New(fps)
        End Sub

        Public Overrides Sub BeginAnimation(ByVal Control As IAnimatable, ByVal TargetPropertyName As String)
            If Not Control.Animations.Contains(Me) Then
                MyBase.BeginAnimation(Control, TargetPropertyName)
                If Not Me.From.HasValue Then
                    Me.From = New Nullable(Of Color)(DirectCast(MyBase.TargetProperty.GetValue(Control, Nothing), Color))
                End If
                Me.Incremental = Me.CalculateIncremental(Me.From.Value, Me.To, MyBase.Duration)
                Me.Value = New Vector4(CSng(Me.From.Value.A), CSng(Me.From.Value.R), CSng(Me.From.Value.G), CSng(Me.From.Value.B))
            End If
        End Sub

        Private Function CalculateIncremental(ByVal Orgion As Color, ByVal [End] As Color, ByVal Time As TimeSpan) As Vector4
            Dim TotalFream As Integer = CInt((Time.TotalSeconds * Me.FPS))
            Dim OrgionV As New Vector4(CSng(Orgion.A), CSng(Orgion.R), CSng(Orgion.G), CSng(Orgion.B))
            Dim EndV As New Vector4(CSng([End].A), CSng([End].R), CSng([End].G), CSng([End].B))
            Return (EndV - OrgionV) / CSng(TotalFream)
        End Function

        Public Overrides Sub OnComplete()
            MyBase.TargetProperty.SetValue(MyBase.TargetObject, Me.To, Nothing)
            MyBase.OnComplete()
        End Sub

        Public Overrides Sub Updata(ByVal gameTime As GameTime)
            If MyBase.IsEnabled Then
                If (Not MyBase.EasingFunction Is Nothing) Then
                    Me.Incremental = DirectCast(MyBase.EasingFunction.GetIncremental(Me.Incremental, MyBase.Progress, MyBase.Duration), Vector4)
                End If
                Me.Value += Me.Incremental * MyBase.Coefficient
                MyBase.TargetProperty.SetValue(MyBase.TargetObject, New Color(Me.Value), Nothing)
                MyBase.Updata(gameTime)
            End If
        End Sub

    End Class

End Namespace